/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.sensor;

import java.awt.Color;
import nl.cloudfarming.client.geoviewer.Palette;
import org.netbeans.api.visual.model.ObjectState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Timon Veenstra
 */
public class SensorPalette implements Palette {

    public static SensorPalette PALLET01_04 = new SensorPalette(0.1, 0.4);
    public static SensorPalette PALLET01_06 = new SensorPalette(0.1, 0.6);
    public static SensorPalette PALLET01_08 = new SensorPalette(0.1, 0.8);
    public static SensorPalette PALLET02_04 = new SensorPalette(0.2, 0.4);
    public static SensorPalette PALLET02_06 = new SensorPalette(0.2, 0.6);
    public static SensorPalette PALLET02_08 = new SensorPalette(0.2, 0.8);
    public static SensorPalette PALLET00_10 = new SensorPalette(0.0, 1.0);
    private double rangeFrom;
    private double rangeTo;
    private static int nrOfColors = 20;
    private static Logger log = LoggerFactory.getLogger(SensorPalette.class);

    public SensorPalette(double rangeFrom, double rangeTo) {
        this.rangeFrom = rangeFrom;
        this.rangeTo = rangeTo;
    }

    @Override
    public Color getColorForValue(Object value) {
        if (value instanceof Double) {
            double dvalue = (Double) value;
            double step = (rangeTo - rangeFrom) / (nrOfColors - 1);

            if (dvalue > 0d){
                log.debug("value "+dvalue);
            }

            if (dvalue < 0d) {
                return Color.BLACK;
            } else if (dvalue <= rangeFrom) {
                return new Color(0x8F050C);
            } else if (dvalue <= rangeFrom + (1 * step)) {
                return new Color(0xB60A06);
            } else if (dvalue <= rangeFrom + (2 * step)) {
                return new Color(0xDB0F01);
            } else if (dvalue <= rangeFrom + (3 * step)) {
                return new Color(0xE32600);
            } else if (dvalue <= rangeFrom + (4 * step)) {
                return new Color(0xE24000);
            } else if (dvalue <= rangeFrom + (5 * step)) {
                return new Color(0xE65E00);
            } else if (dvalue <= rangeFrom + (6 * step)) {
                return new Color(0xF17F00);
            } else if (dvalue <= rangeFrom + (7 * step)) {
                return new Color(0xFC9F00);
            } else if (dvalue <= rangeFrom + (8 * step)) {
                return new Color(0xFDB703);
            } else if (dvalue <= rangeFrom + (9 * step)) {
                return new Color(0xFAD007);
            } else if (dvalue <= rangeFrom + (10 * step)) {
                return new Color(0xF7E80B);
            } else if (dvalue <= rangeFrom + (11 * step)) {
                return new Color(0xDCF40A);
            } else if (dvalue <= rangeFrom + (12 * step)) {
                return new Color(0xB7F805);
            } else if (dvalue <= rangeFrom + (13 * step)) {
                return new Color(0x92FC00);
            } else if (dvalue <= rangeFrom + (14 * step)) {
                return new Color(0x69E003);
            } else if (dvalue <= rangeFrom + (15 * step)) {
                return new Color(0x41C405);
            } else if (dvalue <= rangeFrom + (16 * step)) {
                return new Color(0x16A508);
            } else if (dvalue <= rangeFrom + (17 * step)) {
                return new Color(0x048608);
            } else if (dvalue <= rangeFrom + (18 * step)) {
                return new Color(0x046707);
            } else if (dvalue <= rangeTo) {
                return new Color(0x044606);
            }
        }
        return Color.GRAY;

    }

    @Override
    public String toString() {
        return "SensorDataPallet " + rangeFrom + " -> " + rangeTo;
    }

    @Override
    public Color getColorForState(ObjectState state) {
        return Color.BLACK;
    }
}
